package com.xiashitech.apiservice.dto;

import lombok.*;
import lombok.experimental.Accessors;
import static com.xiashitech.apiservice.Constants.ErrorCode.*;

import com.xiashitech.apiservice.Constants.ErrorCode;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class Result<T> {
    private int status;
    private String message;
    private T data;

    public boolean hasData() {
        return data != null;
    }

    public boolean isSuccess() {
        return Integer.parseInt(RESULT_1001.getCode()) == status;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(Integer.parseInt(RESULT_1001.getCode()), RESULT_1001.getMessage(), data);
    }

    public static <T> Result<T> failure() {
        return failure(RESULT_4001);
    }

    public static <T> Result<T> failure(ErrorCode error) {
        return new Result<>(Integer.parseInt(error.getCode()), error.getMessage(), null);
    }
}
